<?php
class KxPlatform_Api
{
	private $api_key = null;
	private $secret = null;
	private $session_key = null;
	private $v = null;
	public function __construct($config)
	{
		if(isset($config['api_key']))
		{
			$this->api_key = $config['api_key'];
		}
		if(isset($config['secret']))
		{
			$this->secret = $config['secret'];
		}
		if(isset($config['session_key']))
		{
			$this->session_key = $config['session_key'];
		}
		if(isset($config['v']))
		{
			$this->v = $config['v'];
		}
		if(isset($config['actionsurl']))
		{
			$this->actionsurl = $config['actionsurl'];
		}
	}
	public function buildQuery($param)
	{
		$param['api_key'] = $this->api_key;
		$param['call_id'] = microtime(true);
		$param['session_key'] = $this->session_key;
		$param['v'] = $this->v;


		ksort($param);
		$request_str = '';
		foreach ($param as $key => $value)
		{
			$request_str .= $key . '=' . $value;
		}
		$sig = $request_str . $this->secret;
		$sig = md5($sig);
		$param['sig'] = $sig;
		$query = http_build_query($param);
		return $query;
	}
	public function checkSignFromQuery($query)
	{
		$query = explode('&sig=', $query);
		$localsig = md5($query[0].$this->secret);
		if($localsig == $query[1])
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	public function checkSessionKey($sig)
	{
		$localsig = md5($this->session_key.'_'.$this->secret);
		if ($localsig == $sig)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	public static function postRequest($url,$query)
	{
		$post_string = $query;
		if (function_exists('curl_init'))
		{
			$useragent = 'kaixin001.com API PHP5 Client 1.1 (curl) ' . phpversion();
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_URL, $url);
			if (strlen($post_string) >= 3)
			{
				curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
			}
			//use https
			//curl_setopt($ch, CURLOPT_USERPWD, "username:password");
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
			curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
			curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
			curl_setopt($ch, CURLOPT_TIMEOUT, 30);
			$result = curl_exec($ch);
			curl_close($ch);
		}
		else
		{
			$content_type = 'application/x-www-form-urlencoded';
			$content = $post_string;
			$result = self::runHttpPost($content_type,$content,$url);
		}
		return $result;
	}
	public static function runHttpPost($content_type, $content, $server_addr) {
		$user_agent = 'kaixin001.com API PHP5 Client 1.1 (non-curl) ' . phpversion();
		$content_length = strlen($content);
		$context = array(
			'http' => array(
				'method' => 'GET',
				'user_agent' => $user_agent,
				'header' => 'Content-Type: ' . $content_type . "\r\n" .
				'Content-Length: ' . $content_length,
				'content' => $content
			)
		);
		$context_id = stream_context_create($context);
		$sock = fopen($server_addr, 'r', false, $context_id);
		$result = '';
		if ($sock)
		{
			while (!feof($sock))
			{
				$result .= fgets($sock, 4096);
			}
			fclose($sock);
		}
		return $result;
	}
	
	public static function url_base64_encode($str)
	{
		$search = array ('+', '/', '=');
		$replace = array ('*', '-', '');
		$basestr = base64_encode($str);
		return str_replace( $search, $replace, $basestr );
	}
	
	public static function url_base64_decode($str)
	{
		$search = array ('*', '-');
		$replace = array ('+', '/');
		return base64_decode( str_replace( $search, $replace, $str ) );
	}
	
	public static function addFileLog($file,$desc)
	{
		$filename = "./log/".$file.".filelog";
		$fp = fopen($filename, "a");
		if ($fp)
		{
			$pid = getmypid().".".rand(0, 100000);
			$time = date("Y-m-d H:i:s");
			$flog = sprintf("%s\t%s\t%s\n",
				$time, $pid, $desc);
			fwrite($fp, $flog);
			fclose($fp);
		}
	}
	//获取当前用户信息
	//'uids' => '109671659,109676285', 
	public function getUserInfo($uids){
		if ($uids) {
			$param = array(
				'method' => 'users.getInfo',
				'format' => 'json',
				'mode' => 0,
				'uids' => $uids, 
			);
			$query = $this->buildQuery($param);
			$result = $this->postRequest($this->actionsurl, $query);
			$result_decode = json_decode($result, true);
			return $result_decode;
		}
		return false;
	}
	//获取当前登录用户uid
	//'uids' => '109671659,109676285', 
	public function getLoggedInUser(){
		$param = array(
			'method' => 'users.getLoggedInUser',
			'format' => 'json',
			'mode' => 0,
		);
		$query = $this->buildQuery($param);
		$result = $this->postRequest($this->actionsurl, $query);
		$result_decode = json_decode($result, true);
		return $result_decode['result'];
	}
}
?>
